package com.example.zytj.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.zytj.mapper.IConsumeLogMapper;
import com.example.zytj.mapper.IMemberMapper;
import com.example.zytj.model.ConsumeLog;
import com.example.zytj.model.Member;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@ApiOperation("日志信息")
@RequestMapping("log")
public class ConsumeLogController {
    @Resource
    private IConsumeLogMapper mapper;
    @Resource
    private IMemberMapper mmpper;

    @ApiOperation(value = "根据资源室统计数据", notes = "查看每个资源室有多少人在线")
    @GetMapping("room")
    public Map<String, Object> findByRoom() {
        Map<String, Object> map = new HashMap<>();
        return map;
    }

    @ApiOperation(value = "刷卡", notes = "刷卡时生成日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardNo",value = "刷卡人的会员卡号"),
            @ApiImplicitParam(name = "no", value = "卡机编号，1表示进去，2表示出来"),
            @ApiImplicitParam(name = "roomId", value = "刷卡时进出的资源室编号",defaultValue = "0")
    })
    @PutMapping
    public Integer swiping(String cardNo,int no, int roomId) {
        Integer n = 0;
        ConsumeLog cl = new ConsumeLog();
        cl.setCardNo(cardNo);
        cl.setReadRoomId(roomId);
        if(no==1){//刷卡进入
            //1.添加一条日志
            n = mapper.insert(cl);
            //2.修改持卡人会员卡状态
            UpdateWrapper<Member> uw = new UpdateWrapper<>();
            uw.eq("card_no", cardNo);
            uw.set("status", roomId);
            mmpper.update(null, uw);
        }
        if(no==2){//刷卡出来
            //1、修改之日表出来的时间
            n = mapper.swipingOut(cardNo);
            //2、会员卡状态归0
            UpdateWrapper<Member> uw = new UpdateWrapper<>();
            uw.eq("card_no", cardNo);
            uw.set("status", 0);//0表示已出来
            mmpper.update(null, uw);
        }
        return n;
    }
}
